﻿using LeetCodeRecord.ClassFolder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LeetCodeRecord
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Init();
        }

        void Init()
        {
            var source = CreatePages(typeof(MainWindow)).ToList();
            pageList.ItemsSource = source;

            pageList.SelectionChanged += (s, e) =>
            {
                if(pageList.SelectedItem is PageInfo info)
                {
                    var page = Activator.CreateInstance(info.Page);
                    myFrame.Navigate(page);
                }
            };
        }

        /// <summary>
        /// 反射指定条件的类
        /// </summary>
        /// <param name="assemblyType"></param>
        /// <returns></returns>
        IEnumerable<PageInfo> CreatePages(Type assemblyType)
        {
            //程序集
            Assembly assembly = assemblyType.Assembly;
            //获取程序集里面的所有类的定义
            var typeInfos = assembly.GetTypes();

            foreach (TypeInfo typeInfo in typeInfos)
            {
                //是指定接口的类型
                if (typeInfo.GetCustomAttribute(typeof(TopicMessage)) !=null)
                {
                    //var attributes = typeInfo.Attributes;
                    //var ass = typeInfo.GetCustomAttributes(false);
                    //var data = typeInfo.GetCustomAttributesData();
                    var topicMessage = typeInfo.GetCustomAttribute<TopicMessage>();

                    yield return new PageInfo() { Day = topicMessage.Day, Page = typeInfo, Title = topicMessage.Title };
                }
            }
        }
    }

    

}
